package org.springforge.forgemailinglist.impl;

import java.util.Properties;
import java.util.Date;


import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session; 
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.*;

import org.springforge.forgemailinglist.ForgeMailingList;

public class ForgeMailingListImpl implements ForgeMailingList{
 
  public void sendMail(String objet, String corps, String from, String to) throws AddressException, MessagingException 
  {
	    String subject = null,text = null,cc = null, bcc = null;
		String mailhost = null;
		String mailer = "Forge";
		String user = null, password = null, prot = null;

		prot = "smtps";
		mailhost = "smtp.gmail.com";
		user = "forgecpe";
		password = "forge2010"; 
		//from = "forgecpe@gmail.com";
		//to = "ced.mathieu@gmail.com";
		subject = " test par bundle";
		text = "ca a marcher cool";
		
		try {
		    
		    //Initialize the JavaMail Session.
		    Properties props = System.getProperties();
		    props.put("mail." + prot + ".host", mailhost);
			props.put("mail." + prot + ".auth", "true");

		    // Get a Session object
		    Session session = Session.getInstance(props, null);

		    //Construct the message.
		    Message msg = new MimeMessage(session);
		    if (from != null)
		    	msg.setFrom(new InternetAddress(from));
		    else
		    	msg.setFrom();

		    msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
		    if (cc != null)
		    	msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc, false));
		    if (bcc != null)
		    	msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc, false));

		    msg.setSubject(objet);
			msg.setText(corps);
		    msg.setHeader("Mailer", mailer);
		    msg.setSentDate(new Date());

		    //Send the message
		    
		    //The simple way to send a message is this:
		    //Transport.send(msg);
		    
		    SMTPTransport t =(SMTPTransport)session.getTransport(prot);
		    try {
			    t.connect(mailhost, user, password);
			    t.sendMessage(msg, msg.getAllRecipients());
		    } 
		    finally {
		    	System.out.println("Response: " + t.getLastServerResponse());
		    	t.close();
		    }
		    
		    System.out.println("\nMail was sent successfully.");

		} catch (Exception e) {
// Handle SMTP-specific exceptions.

		    if (e instanceof SendFailedException) {
			MessagingException sfe = (MessagingException)e;
			if (sfe instanceof SMTPSendFailedException) {
			    SMTPSendFailedException ssfe =
					    (SMTPSendFailedException)sfe;
			    System.out.println("SMTP SEND FAILED:");
				System.out.println(ssfe.toString());
			    System.out.println("  Command: " + ssfe.getCommand());
			    System.out.println("  RetCode: " + ssfe.getReturnCode());
			    System.out.println("  Response: " + ssfe.getMessage());
			} 
			else {
				System.out.println("Send failed: " + sfe.toString());
			}
			Exception ne;
			while ((ne = sfe.getNextException()) != null &&	ne instanceof MessagingException) {
			    sfe = (MessagingException)ne;
			    if (sfe instanceof SMTPAddressFailedException) {
			    	SMTPAddressFailedException ssfe =(SMTPAddressFailedException)sfe;
			    	System.out.println("ADDRESS FAILED:");
			    	System.out.println(ssfe.toString());
					System.out.println("  Address: " + ssfe.getAddress());
					System.out.println("  Command: " + ssfe.getCommand());
					System.out.println("  RetCode: " + ssfe.getReturnCode());
					System.out.println("  Response: " + ssfe.getMessage());
			    } else if (sfe instanceof SMTPAddressSucceededException) {
					System.out.println("ADDRESS SUCCEEDED:");
					SMTPAddressSucceededException ssfe =(SMTPAddressSucceededException)sfe;
					System.out.println(ssfe.toString());
					System.out.println("  Address: " + ssfe.getAddress());
					System.out.println("  Command: " + ssfe.getCommand());
					System.out.println("  RetCode: " + ssfe.getReturnCode());
					System.out.println("  Response: " + ssfe.getMessage());
			    }
			}
		    } else {
			System.out.println("Got Exception: " + e);
			e.printStackTrace();
		    }
		}
	    }
}
